package org.luxor.commons.playwright;

import com.microsoft.playwright.Page;
import org.luxor.commons.core.utils.UuidUtils;
import org.luxor.commons.playwright.component.Context;
import org.luxor.commons.playwright.component.command.PlaywrightCommand;
import org.luxor.commons.playwright.listener.AbstractPlaywrightEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Playwright 事件监听器抽象类
 *
 * @author Mr.yan  @date 2022/4/10
 */
@Component
public class MePlaywrightEventListener extends AbstractPlaywrightEventListener {
    private static final Logger log = LoggerFactory.getLogger(MePlaywrightEventListener.class);

    /**
     * 初始化执行指令
     *
     * @param command 业务指令
     * @param context 业务上下文
     */
    @Override
    public void initExecute(PlaywrightCommand command, Context context) {
        log.info("Enter initExecute Event. ");
        context.setTrackId(UuidUtils.uuid32());
        context.setSessionId(UuidUtils.uuid());
    }

    /**
     * 指令执行之前
     *
     * @param page    当前页
     * @param command 业务指令
     * @param context 业务上下文
     */
    @Override
    public void beforeExecute(Page page, PlaywrightCommand command, Context context) throws Exception {
        log.info("Enter beforeExecute Event. ");
    }

    /**
     * 指令执行之后
     *
     * @param page    当前页
     * @param command 业务指令
     * @param context 业务上下文
     */
    @Override
    public void afterExecute(Page page, PlaywrightCommand command, Context context) throws Exception {
        log.info("Enter afterExecute Event.");
    }

    /**
     * 异常处理
     *
     * @param throwable 异常
     * @param page      当前页
     */
    @Override
    public void onException(Throwable throwable, Page page) {
        log.info("Enter onException Event.");
    }

}
